<?php

class Admini_ManageuserController extends Uuch_ManageController {

    public function init() {
        parent::init();
        $this->appendCss('/css/Share.css');
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->view->transferCount = Application_Model_Remittance::getTransferCount();
        $this->view->complaintCount = Application_Model_Complaints::getComplaintCount();
        $this->view->type = "user";
    }

    public function indexAction() {
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("用户管理");
        $quick = $this->getSafeParam("quick");
        $dateselect = $this->getSafeParam('dateselect');
        $starttime = $this->getSafeParam('starttime');
        $endtime = $this->getSafeParam('endtime');
        $cityid = $this->getSafeParam("cityid");
        $cityname = $this->getSafeParam("cityname");
        $orderby = $this->getSafeParam("orderby");
        $userSearch = new Application_Model_Filter_UserSearch();
        $userSearch->quick = $quick;
        $userSearch->dateselect = $dateselect;
        $userSearch->starttime = $starttime;
        $userSearch->endtime = $endtime;
        $userSearch->cityid = $cityid;
        $userSearch->order = $orderby;
        $userall = Application_Model_User::getselcount($userSearch, $pagesize, $pageindex);
        $words = array();
        for ($i = 0; $i < 26; $i++) {
            $words[] = chr(65 + $i);
        }
        $this->view->words = $words;
        $this->view->quick = $quick;
        $this->view->dateselect = $dateselect;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->userall = $userall;
        $this->view->cityid = $cityid;
        $this->view->cityname = $cityname;
        $this->view->orderby = $orderby;
        $useralllist = $this->view->userall['list'];
        $this->view->useralllist = $useralllist;
        $this->view->stype = 1;
    }

   public function uwaybillsAction() {
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("用户活跃情况统计");
        $quick = $this->getSafeParam("quick");
        $years = $this->getSafeParam("years");
        $dq_years = date('Y');
        if(!$years){
           $years = $dq_years; 
        }
        $conditions = "where w.status > 0 and year(datePaid) = ".$years;
        if($quick != ""){
            $conditions .= " and (nikename like '%".$quick."%' or email like '%".$quick."%' or phone like '%".$quick."%' or storeno like '%".$quick."%')"; 
        }
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $userids = $db->fetchOne(
                    "SELECT count(1) FROM (SELECT count(w.id) as num FROM uuch_waybill as w left join uuch_user as u on u.id = w.userId ".
                      $conditions . " group by w.userId HAVING num > 0) uw "
                    );

        $userlist = $db->fetchAll(
                    "SELECT w.userId,u.storeno,count(w.id) as num FROM uuch_waybill as w left join uuch_user as u on u.id = w.userId ".
                      $conditions . " group by w.userId HAVING num > 0 order by num desc LIMIT $pageindex, $pagesize"
                    );
        $size = $userids;

        $pages = intval($size / $pagesize);
        $pages = $pages + ($size % $pagesize != 0 ? 1 : 0);
        $index = $pageindex / $pagesize;

        $userlistAll = array('size' => $size, 'list' => $userlist, 'pages' => $pages, 'index' => $index, 'pagesize' => $pagesize);
        $this->view->userlistAll = $userlistAll;
        $this->view->userlist = $userlist;
        $this->view->size = $size;
        $this->view->quick = $quick;
        $this->view->years = $years;
    }

    //年费会员列表
    public function nfhyAction() {
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize ? $pagesize : 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("年费会员");
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $userids = $db->fetchAll(
             "SELECT u.id FROM uuch_user as u LEFT JOIN uuch_account as a on a.id = u.id ".
            " WHERE a.dateUp > 0 order by a.dateUp desc" 
         );      
        $size = count($userids);
        $userlist = $db->fetchAll(
             "SELECT u.*,a.score,a.money,a.dateUp FROM uuch_user as u LEFT JOIN uuch_account as a on a.id = u.id ".
            " WHERE a.dateUp > 0  order by a.dateUp desc LIMIT $pageindex, $pagesize" 
         );
        $pages = intval($size / $pagesize);
        $pages = $pages + ($size % $pagesize != 0 ? 1 : 0);
        $index = $pageindex / $pagesize;

        $userlistAll = array('size' => $size, 'list' => $userlist, 'pages' => $pages, 'index' => $index, 'pagesize' => $pagesize);
        $this->view->userlistAll = $userlistAll;
        $this->view->userlist = $userlist;
        $this->view->size = $size;
    }

    //2016-09-01至2016-10-07 代购金额统计
    public function dgtjAction() {
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize ? $pagesize : 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("中秋国庆代购统计");
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $ordersum = $db->fetchAll(
             "SELECT o.userId,u.email,u.storeno,u.nikename,a.dateUp,a.hdtag,sum(o.sum) as sum FROM `uuch_order` as o left join uuch_user as u on u.id = o.userId left join uuch_account as a on a.id = o.userId WHERE  STATUS in (1,2,3,4,5) AND (datePaid >= '2016-09-01' && datePaid <= '2016-10-07') group by userId HAVING sum > 2000 order by sum desc  LIMIT $pageindex, $pagesize"
         );      
        //$userId = 48214;
        $re = array();
        foreach ($ordersum as $k=>$value) {
            $userId = $value['userId'];
            //补交款
            $repayMoneysql = 'SELECT SUM(l.repayMoney) AS sum FROM `uuch_repayLink` AS l LEFT JOIN `uuch_order` AS o ON l.`relationId` = o.`id` WHERE l.userId = '. 
            $userId . ' AND l.type = 2 AND l.status = 1 AND (l.datePaid >= "2016-09-01" && l.datePaid <= "2016-10-07")';
            $repayMoney = $db->fetchOne($repayMoneysql);

            //手工充值
            $sgczsql = 'SELECT sum(money) as sum FROM `uuch_financialCheckItem` WHERE userId = '. 
            $userId . ' AND needAddMoney in (2,5) AND (dateCreated >= "2016-09-01" && dateCreated <= "2016-10-07")';
            $sgcz = $db->fetchOne($sgczsql);

            //手工扣款
            $sgkksql = 'SELECT sum(money) as sum FROM `uuch_financialCheckItem` WHERE userId = '. 
            $userId . ' AND needAddMoney in (1,4) AND (dateCreated >= "2016-09-01" && dateCreated <= "2016-10-07")';
            $sgkk = $db->fetchOne($sgkksql);

            //单项退款
            $dxsql = 'SELECT sum(afterMoney-beforeMoney) as sum FROM `uuch_account_history` WHERE accountId = '. 
            $userId . ' AND comment like "%单项退款%" AND (dateCreated >= "2016-09-01" && dateCreated <= "2016-10-07")';
            $dx = $db->fetchOne($dxsql);
            $qt = $repayMoney - $sgcz + $sgkk - $dx;
            $re[$k]['sum'] = $value['sum'] + $qt;
        }
        //var_dump($ordersum);exit;
        $size = 36;
        $pages = intval($size / $pagesize);
        $pages = $pages + ($size % $pagesize != 0 ? 1 : 0);
        $index = $pageindex / $pagesize;

        $order = array('size' => $size, 'list' => $ordersum, 'pages' => $pages, 'index' => $index, 'pagesize' => $pagesize);
        $this->view->order = $order;
        $this->view->ordersum = $ordersum;
        $this->view->re = $re;
    }

 //最近3天同一ip用户
 public function ipsameAction(){
        $this->setTitle("近三天同一ip登入用户");
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $sj = date('Y-m-d',strtotime('- 1 day ',time()));
        $txt = APPLICATION_PATH . '/../data/ipsame/' . date('Y-m-d') . '.txt';        
        $userlist = $db->fetchAll(
             "SELECT loginip,count(id) as num FROM uuch_user where lastLogin >= '$sj' and userType = '0' group by loginip HAVING num>1 order by num desc"
         );
        error_log(date('Y-m-d H:i:s'). "\r\n", 3, $txt);
        foreach ($userlist as $k => $value) {
            $userinfo[$k] = $db->fetchAll(
                 "SELECT id,email,storeno,lastLogin FROM uuch_user where loginip = '".$value["loginip"]."' and lastLogin >= '".$sj."' and userType = '0'  order by id desc"
             );
            $info = var_export($userinfo[$k],TRUE);
            error_log($value["loginip"]. "\r\n".$info."\r\n\r\n\r\n", 3, $txt);
       }
        $this->view->userlist = $userlist;
        $this->view->userinfo = $userinfo;
    }


    //AJAX请求
    public function userqueryAction() {
        $username = $this->_getParam('username');
        $email = $this->_getParam('email');
        $storeno = $this->_getParam('storeno');
        $starttime = $this->_getParam('starttime');
        $endtime = $this->_getParam('endtime');
        $dateselect = $this->_getParam('dateselect');
        $pagesize = $this->_getParam('pagesize');
        $pageindex = $this->_getParam('pageindex');
        $item = new Application_Model_User();
        $result = $item->getUserAll($username, $email, $storeno, $starttime, $endtime, $dateselect, $pagesize, $pageindex);
        echo '{"count":"' . $result[0] . '","items":' . json_encode($result[1]) . '}';
        exit;
    }

    public function userpagerAction() {
        $username = $this->_getParam('username');
        $email = $this->_getParam('email');
        $storeno = $this->_getParam('storeno');
        $starttime = $this->_getParam('starttime');
        $endtime = $this->_getParam('endtime');
        $dateselect = $this->_getParam('dateselect');
        $pagesize = $this->_getParam('pagesize');
        $pageindex = $this->_getParam('pageindex');
        $item = new Application_Model_User();
        $result = $item->getUserAll($username, $email, $storeno, $starttime, $endtime, $dateselect, $pagesize, $pageindex);
        echo json_encode($result[1]);
        exit;
    }

    public function userdetailsAction() {
        $id = $this->_getParam('id');
        $item = new Application_Model_User();
        $result = $item->editGetUser($id);
        echo json_encode($result);
        exit;
    }

    public function usereditAction() {
        $id = $this->_getParam('id');
        $user_set = array(
            'email' => $this->_getParam('useremail'),
            'nikename' => $this->_getParam('profilenickName'),
            'username' => $this->_getParam('username'),
            'userLevel' => (int) $this->_getParam('userlevel'),
        );
        $profile_set = array(
            'home' => $this->_getParam('profilehome'),
            'name' => $this->_getParam('profilename'),
            'address' => $this->_getParam('profileaddress'),
            'lang' => $this->_getParam('profilelang'),
            'birthday' => $this->_getParam('profilebirthday'),
            'sex' => $this->_getParam('profilesex'),
            'description' => $this->_getParam('profiledescription'),
            'email' => $this->_getParam('profileemail'),
            'qq' => $this->_getParam('profileqq'),
            'tel' => $this->_getParam('profiletel'),
            'nickName' => $this->_getParam('profilenickName'),
        );
        $item = new Application_Model_User();
        $result = $item->editUser($id, $user_set, $profile_set);
        echo 'editok';
        exit;
    }

    public function useraddAction() {
        $row = array(
            'cityid' => (int) $this->_getParam('cityid'),
            'shipTypeId' => (int) $this->_getParam('shipTypeId'),
            'agentCompanyId' => (int) $this->_getParam('agentCompanyId'),
            'discount' => $this->_getParam('discount'),
            'price' => $this->_getParam('price'),
            'weight' => $this->_getParam('weight'),
            'startweight' => $this->_getParam('startweight'),
            'startPrice' => $this->_getParam('startPrice'),
            'queryurl' => $this->_getParam('queryurl'),
            'oil' => $this->_getParam('oil'),
            'clearancePrice' => $this->_getParam('clearancePrice'),
            'pushTime' => $this->_getParam('pushTime'),
            'state' => $this->_getParam('state'),
        );
        $item = new Application_Model_User();
        $result = $item->addUser($row);
        echo 'addok';
        exit;
    }

    public function userdelAction() {

        $idlist = $this->_getParam('idList');
        $item = new Application_Model_User();
        $result = $item->delUser($idlist);
        echo json_encode(true);
        exit;
    }

    public function userenableAction() {
        $idlist = $this->_getParam('idList');
        $item = new Application_Model_User();
        $result = $item->enableUser($idlist);
        echo json_encode(true);
        exit;
    }

    public function userdisableAction() {
        $idlist = $this->_getParam('idList');
        $item = new Application_Model_User();
        $result = $item->disableUser($idlist);
        echo json_encode(true);
        exit;
    }

    public function accountAction() {

        $count = 15;
        $start = $this->_getParam('start');
        $quick = $this->_getParam("quick");
        $dateinbegin = $this->_getParam('dateinbegin');
        $dateinend = $this->_getParam('dateinend');
        $status = $this->getSafeParam("status");
        if ($status) {
            $this->view->stype = 22;
            $this->setTitle("成长值明细");
        } else {
            $status = Application_Model_UserAccountHistory::STATE_MONEY;
            $this->view->stype = 2;
            $this->setTitle("账目明细");
        }
        if (!$start) {
            $start = 0;
        }
        $arr = new Application_Model_Filter_UseraccountSearch();
        $arr->quick = $quick;
        $arr->stime = $dateinbegin;
        $arr->etime = $dateinend;
        $arr->state = $status;
        $this->view->Accounts = Application_Model_UserAccountHistory::getMyFilter($arr,$count, $start);
        $this->view->Accountlist = $this->view->Accounts['list'];
        $this->view->quick = $quick;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->status = $status;
    }

    /**
     * 用户地址管理
     */
    public function addressAction() {
        $count = 15;
        $start = $this->_getParam('start');
        $quick = $this->_getParam('quick');
        $this->setTitle("会员地址薄");
        if (!$start) {
            $start = 0;
        }
        $this->view->Address = Application_Model_address::getByFilter($quick, $count, $start);
        $this->view->AddressList = $this->view->Address['list'];
        $this->view->quick = $quick;
        $this->view->stype = 3;
    }

    public function editAction() {
        $id = $this->_getParam('id');
        if ($this->_getParam('operate') == 'edituser') {
            $realname = $this->_getParam('realname');
            $nikename = $this->_getParam('nikename');
            $uemail = $this->_getParam('uemail');
            $score = $this->_getParam('score');
            $profile_set = array(
                'home' => $this->_getParam('home'),
                'name' => $this->_getParam('name'),
                'address' => $this->_getParam('address'),
                'lang' => $this->_getParam('lang'),
                'birthday' => $this->_getParam('birthday'),
                'sex' => $this->_getParam('sex'),
                'description' => $this->_getParam('description'),
                'email' => $this->_getParam('email'),
                'qq' => $this->_getParam('qq'),
                'tel' => $this->_getParam('tel'),
                'nickName' => $this->_getParam('nickName'),
            );

            $item = new Application_Model_User();
            $result = $item->editUserDetail($id, $realname, $nikename, $uemail, $score, $profile_set);
            if ($result) {
                $this->view->message = 1;
            } else {
                $this->view->message = "发生未知错误，请重新修改！";
            }
        }
        $user = new Application_Model_User();
        $userdetail = $user->getUserDetail($id);
        $profile = new Application_Model_Profile();
        $profiledetail = $profile->getByUserId($id);
        $this->view->userdetail = $userdetail;
        $this->view->profile = $profiledetail;
    }

    public function repwdAction() {
        $id = $this->_getParam('id');
        $userItem = Application_Model_User::load($id);
        $xpwd = md5(md5('123456') . $userItem->passwordsalt);
        $userItem->password = $xpwd;
        $userItem->save();
        Application_Model_Operate::addOperate(0, null, $userItem->storeno, "重置用户密码", $id);
        echo true;
        exit;
    }

    /**
     * 消息列表
     */
    public function mailAction() {
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("系统站内信");
        $nikename = $this->_getParam('username');
        $starttime = $this->_getParam('dateinbegin');
        $endtime = $this->_getParam('dateinend');
        $mailall = Application_Model_Mail::getselmail($nikename, $starttime, $endtime, $pagesize, $pageindex);
        $this->view->username = $nikename;
        $this->view->dateinbegin = $starttime;
        $this->view->dateinend = $endtime;
        $this->view->mailall = $mailall;
        $mailalllist = '';
        if(isset($mailall['list'])){
            $mailalllist = $this->view->mailall['list'];
        }
        $this->view->mailalllist = $mailalllist;
        $this->view->stype = 4;
    }

    /**
     * 邮件列表
     */
    public function emailAction() {
        $pagesize = 15;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle("邮件明细");
        $nikename = $this->_getParam('username');
        $emailall = Application_Model_Email::getselemail($nikename, $pagesize, $pageindex);
        $this->view->username = $nikename;
        $this->view->emailall = $emailall;
        $emailalllist = $this->view->emailall['list'];
        $this->view->emailalllist = $emailalllist;
        $this->view->stype = 5;
    }

     public function sendemailAction(){
       $id = $this->_getParam('id'); 
       if($id)       $emailall = Application_Model_Email::load($id);
       else $emailall = '';

       if($this->isPost()){
        $title = $this->_getParam('title');
        $email = $this->_getParam('username');
        $body = $this->_getParam('message');

        if(preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i",$email)){
                $receive = array('email' => $email, 'name' => $email);
               $ok=Uuch_Mail2::SendMail($title, $body, $receive);
               if($ok) $this->view->ma = 1;
         } 
       }
        $this->view->emailall = $emailall;
        $this->view->stype = 5;
       }

    /**
     * 发布新消息
     */
    
    public function releasemgAction() {
        $userId = $this->_getParam('userId');
        $this->setTitle("发布站内信");
        if ($userId) {
            $result = Application_Model_User::load($userId);
            $this->view->userresult = $result;
        }
        $motion = $this->_getParam('motion');
        if ($motion == 'remessage') {
            if ($result) {
                $title = $this->_getParam('title');
                $type = $this->_getParam('type');
                $message = $this->_getParam('message');
                $mailItem = new Application_Model_Mail();
                $mailItem->title = $title;
                $mailItem->userId = $userId;
                $mailItem->type = $type;
                $mailItem->message = $message;
                $mailItem->status = 0;
                $mailItem->dateCreated = date('Y-m-d H:i:s');
                $mailItem->save();
                Application_Model_Operate::addOperate(0, null, $mailItem->id, "发送站内信", $mailItem->userId);
                $this->view->ma = 1;
            } else {
                $this->view->ma = 2;
            }
        }
        $this->view->userid = $userId;

        $this->view->stype = 4;
    }
    
    /**
     * 群发新消息
     */
    public function releasemgallAction() {
        $userId = $this->getSafeParam('userId');
        $this->setTitle("发布站内信");
        if ($userId) {
            $result = Application_Model_User::load($userId);
            $this->view->userresult = $result;
        }
        $this->view->userid = $userId;

        $this->view->stype = 4;
        if (!$this->isPost()) {
            return ;
        }
        $releaseType = $this->getSafeParam("releaseType");//发布类型 1个人 2群发
        $title = $this->getSafeParam('title');
        $type = $this->getSafeParam('type');
        $message = $this->_getParam('message');
        $content = preg_replace("/(\r)*\n/", "<br/>", preg_replace("/\s/"," ",$message));
        if($releaseType == 1){
            if (!$result) {
                $this->error("不存在该用户，请重新输入");
                return ;
            }
            $mailItem = new Application_Model_Mail();
            $mailItem->title = $title;
            $mailItem->userId = $userId;
            $mailItem->type = $type;
            $mailItem->message = $content;
            $mailItem->status = 0;
            $mailItem->dateCreated = date('Y-m-d H:i:s');
            $mailItem->save();
            $this->view->ma = 1;
            return ;
        }
        Application_Model_Mail::releasemgaAll($title,$type,$message,$content);
        $this->view->ma = 1;
    }

    /**
     * 获取用户仓库号
     */
    public function bystorenoAction() {
        $nikename = $this->_getParam('username');
        $result = Application_Model_User::getbynikename($nikename);
        if ($result) {
            $this->json(array('id' => $result->id, 'storeno' => $result->storeno));
        } else {
            $this->json(array('id' => '', 'storeno' => ''));
        }
    }

    /**
     * 获取用户昵称
     */
    public function byusernameAction() {
        $storeno = $this->_getParam('storeno');
        $result = Application_Model_User::bystore($storeno);
        if ($result) {
            $this->json(array('id' => $result->id,'username' => $result->nikename,'email' => $result->email));
        } else {
            $this->json(array('id' => '', 'username' => ''));
        }
    }

    /**
     * 用户详情
     */
    public function userdetailAction() {
        $id = $this->_getParam('id');
        $operate = $this->_getParam('operate');
        if ($operate == 'addremark') {
            $userresult = Application_Model_User::load($id);
            $remark = $this->_getParam('remark');
            $userresult->remark = $remark;
            Application_Model_Operate::addOperate(0, null, $userresult->storeno, "添加用户备注", $id);
            $userresult->save();
        }
        if ($operate == 'recharge') {
            $userid = $this->_getParam("userid");
            $money = $this->_getParam("money");
            $reason = $this->_getParam("reason");
            $message = $this->_getParam('message');
            $result = Application_Model_User::editmoney($userid, $money, $reason, $message, $operate);
            $this->view->ma = $result;
            $id = $userid;
        }
        if ($operate == 'deduct') {
            $userid = $this->_getParam("userid");
            $money = $this->_getParam("money");
            $reason = $this->_getParam("reason");
            $message = $this->_getParam('message');
            $result = Application_Model_User::editmoney($userid, $money, $reason, $message, $operate);
            $this->view->ma = $result;
            $id = $userid;
        }
        $userresult = Application_Model_User::load($id);
        $this->view->userdetail = $userresult;
        $this->view->id = $id;
        $this->view->operator = Uuch_Util::getUserNikeName();
        $this->view->stype = 1;
    }

    /*
     * 某一用户的运单详情
     */

    public function billdetailAction() {
        $pagesize = 10;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $id = $this->_getParam('id');
        $WayBillSearch = new Application_Model_Filter_WayBillSearch();
        $WayBillSearch->userid = $id;
        $billresult = Application_Model_wayBillItem::getBillAll($WayBillSearch, $pagesize, $pageindex);
        $this->view->billall = $billresult;
        $this->view->billlist = $billresult['list'];
        $this->_helper->layout->disableLayout();
    }

    /**
     * 某一用户的代购详情
     */
    public function buydetailAction() {
        $pagesize = 2;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $id = $this->_getParam('id');
        $orderSearch = new Application_Model_Filter_OrderSearch();
        $orderSearch->userid = $id;
        $buyresult = Application_Model_Order::getByPages($orderSearch, $pagesize, $pageindex);
        $this->view->buyall = $buyresult;
        $this->view->buylist = $buyresult['list'];
        $this->_helper->layout->disableLayout();
    }

    /**
     * 某一用户的仓库详情
     */
    public function storedetailAction() {
        $pagesize = 10;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $id = $this->_getParam('id');
        $userresult = Application_Model_User::load($id);
        $storeItemSearch = new Application_Model_Filter_StoreItemSearch();
        $storeItemSearch->quick = $userresult->storeno;
        $storeItemSearch->stus = 0;
        //$storeItemSearch->userId = $id;
        $storeresult = Application_Model_StoreItem::getMyByFilter($storeItemSearch, $pagesize, $pageindex);
        $this->view->storeall = $storeresult;
        $this->view->storelist = $storeresult['list'];
        $this->_helper->layout->disableLayout();
    }

    /**
     * 某一用户的金额变动
     */
    public function moneydetailAction() {
        $pagesize = 10;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $id = $this->_getParam('id');
        $userresult = Application_Model_User::load($id);
        $arr = new Application_Model_Filter_UseraccountSearch();
        $arr->quick = $userresult->storeno;
        $arr->state = Application_Model_UserAccountHistory::STATE_MONEY;
        $this->view->Accounts = Application_Model_UserAccountHistory::getMyFilter($arr, $pagesize, $pageindex);
        $this->view->Accountlist = $this->view->Accounts['list'];
        $this->_helper->layout->disableLayout();
    }

    public function couponAction() {
        $this->setTitle("优惠券系统");
        $pageindex = $this->_getParam('start');
        $pagesize = $this->getSafeParam("pagesize")?$this->getSafeParam("pagesize"):15;
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $timescope = $this->getSafeParam("timescope")?$this->getSafeParam("timescope"):"dateCreated";
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        $selType = $this->getSafeParam("selType");
        $scope = $this->getSafeParam("scope");
        $status = $this->getSafeParam("status");
        $batchNo = $this->getSafeParam("batchNo");
        
        $couponSearch = new Application_Model_Filter_CouponSearch();
        $couponSearch->quick = $quick;
        $couponSearch->dateType = $timescope;
        $couponSearch->startTime = $starttime;
        $couponSearch->endTime = $endtime;
        $couponSearch->type = $selType;
        $couponSearch->scope = $scope;
        $couponSearch->status = $status;
        $couponSearch->batchNo = $batchNo;
        $couponItem = Application_Model_Coupon::getByFilter($couponSearch, $pagesize, $pageindex);
        // $db = Zend_Registry::get('db');
        // $db->getProfiler()->setEnabled(true);         
        // $profiler = $db->getProfiler();         
        // var_dump($profiler); exit;
        $this->view->quick = $quick;
        $this->view->timescope = $timescope;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->batchNo = $batchNo;
        $this->view->status = $status;
        $this->view->selType = $selType;
        $this->view->scope = $scope;
        $this->view->pagesize = $pagesize; 
        
        $this->view->couponall = $couponItem;
        $this->view->couponlist = $couponItem["list"];
        
        $this->view->batchAll = Application_Model_CouponBatch::getBatchAll();
        $this->view->couponTypeAll = Application_Model_Coupon::getTypeAll();
        $this->view->arrscope = Application_Model_Coupon::getbyscope();
        $this->view->couponStatusAll = Application_Model_Coupon::getStatusAll();
        if($batchNo){
            $this->view->batchItem = Application_Model_CouponBatch::getByNo($batchNo);
        }
        $this->view->stype = 6;
    }

    public function addcouponAction() {
        $this->setTitle("添加优惠券");
        $arrup = array(
                "userId" => false,
                "username" => false,
                "storeno" => false,
                "no" => false,
                "password" => false,
                "money" => false,
                "couponType" => false,
                "discount" => false,
                "startTime" => false,
                "dateExpired" => false,
                "scope" => false,
                "name" => false,
                "batchNo" => false,
                "common" => false,
            );
        if ($this->_getParam('operate') == "add") {
            $userId = $this->_getParam('userId');
            $discount = $this->_getParam("discount");
            $startTime = ($this->_getParam("startTime") > $this->_getParam("dateExpired")) ? $this->_getParam("dateExpired") : $this->_getParam("startTime");
            $dateExpired = ($this->_getParam("startTime") > $this->_getParam("dateExpired")) ? $this->_getParam("startTime") : $this->_getParam("dateExpired");
            if ($userId != ""  && ($discount != "" && $discount != 0)) {
                $username = $this->getSafeParam("username");
                $storeno = $this->getSafeParam("storeno");
                $money = $this->getSafeParam("money");
                $batchType = $this->getSafeParam("batchType");
                $scope = $this->getSafeParam("scope");
                $name = $this->getSafeParam("name");
//                $no = $this->getSafeParam("no");
//                $password = $this->getSafeParam("password");
                $comment = $this->getSafeParam("common");
                $batchNo = $this->getSafeParam("batchNo");
                $arrup = array(
                    "userId" => $userId,
                    "username" => $username,
                    "storeno" => $storeno,
                    "no" => false,
                    "password" => false,
                    "money" => $money,
                    "discount" => $discount,
                    "startTime" => $startTime,
                    "couponType" => $batchType,
                    "dateExpired" => $dateExpired,
                    "scope" => $scope,
                    "name" => $name,
                    "batchNo" => $batchNo,
                    "common" => $comment,
                );
                $coupontItem = Application_Model_Coupon::addCoupon($userId, $dateExpired, null, $money, $discount, $comment, $batchType, $scope, $startTime, $batchNo, $name);
                Application_Model_Operate::addOperate(0, null, $coupontItem->No, "添加优惠券", $userId);
                $this->view->couponNo = $coupontItem->No;
                $this->view->ma = 1;
            } else {
                $this->view->ma = 3;
            }
        }
        $this->view->arrup = $arrup;
        $this->view->couponTypeAll = Application_Model_Coupon::getTypeAll();
        $this->view->batch = Application_Model_CouponBatch::batchall();
        $this->view->arrscope = Application_Model_Coupon::getbyscope();
        $this->view->stype = 6;
    }

    public function addvouchersAction() {
        $this->setTitle("添加抵用劵");
        if ($this->_getParam('operate') == "add") {
            $userId = $this->getSafeParam('userId');
            $storeno = $this->getSafeParam("storeno");
            $discount = $this->getSafeParam("discount");
            $startTime = ($this->getSafeParam("startTime") > $this->getSafeParam("dateExpired")) ? $this->getSafeParam("dateExpired") : $this->getSafeParam("startTime");
            $dateExpired = ($this->getSafeParam("startTime") > $this->getSafeParam("dateExpired")) ? $this->getSafeParam("startTime") : $this->getSafeParam("dateExpired");
            if ($userId != "" && ($discount != "" && $discount != 0)) {
                $arrup = array(
                    "userId" => $userId,
                    "discount" => $discount,
                    "startTime" => $startTime,
                    "dateExpired" => $dateExpired,
                );
                $result = Application_Model_Coupon::addVouchers($userId, $storeno, $arrup);
                $this->view->arrup = $arrup;
                $this->view->ma = $result;
            } else {
                $this->view->ma = 0;
            }
        }
        $this->view->stype = 6;
    }

    public function coupondetailAction() {
        $this->setTitle("优惠券详情");
        $id = $this->_getParam("id");
        $couponItem = Application_Model_Coupon::load($id);
        $this->view->couponItem = $couponItem;
    }

    public function bycouponnoAction() {
        $no = $this->_getParam("no");
        $result = Application_Model_Coupon::getByNo($no);
        if ($result) {
            echo 2;
        } else {
            echo 1;
        }
        exit;
    }

    public function bybatchnoAction() {
        $no = $this->_getParam("no");
        $result = Application_Model_CouponBatch::getByNo($no);
        if ($result) {
            $this->json(array('sjtype' => 2, 'name' => $result->name, 'money' => $result->money, 'discount' => $result->discount, "type" => $result->type , "scope" => $result->scope,
                'dateStart' => $result->dateStart, 'dateEnd' => $result->dateEnd, 'maxCount' => $result->maxCount, 'giveCount' => $result->giveCount));
        } else {
            $this->json(array('sjtype' => 1));
        }
    }

    public function couponbatchAction() {
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $timescope = $this->getSafeParam("timescope");
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        $pagesize = $this->getSafeParam("pagesize")?$this->getSafeParam("pagesize"):15;
        $selType = $this->getSafeParam("selType");
        $scope = $this->getSafeParam("scope");
        $batchItem = Application_Model_CouponBatch::getByFilter($quick, $timescope, $starttime, $endtime, $selType, $scope, $pagesize, $pageindex);
        $this->view->batchall = $batchItem;
        $this->view->batchlist = $batchItem["list"];
        $this->view->couponTypeAll = Application_Model_Coupon::getTypeAll();
        $this->view->arrscope = Application_Model_Coupon::getbyscope();
        $this->view->quick = $quick;
        $this->view->timescope = $timescope;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->selType = $selType;
        $this->view->scope = $scope;
        $this->view->pagesize = $pagesize;
        $this->view->start = $pageindex;
        $this->view->stype = 6;
    }

    public function addbatchAction() {
        if ($this->_getParam('operate') == "add") {
            $no = $this->getSafeParam("no");
            $discount = $this->getSafeParam("discount");
            $dateStart = ($this->getSafeParam("dateStart") > $this->getSafeParam("dateEnd")) ? $this->getSafeParam("dateEnd") : $this->getSafeParam("dateStart");
            $dateEnd = ($this->getSafeParam("dateStart") > $this->getSafeParam("dateEnd")) ? $this->getSafeParam("dateStart") : $this->getSafeParam("dateEnd");
            if ($no != "" && ($discount != "" && $discount != 0)) {
                $arrup = array(
                    "no" => $no,
                    "name" => $this->getSafeParam("name"),
                    "money" => $this->getSafeParam("money")?$this->getSafeParam("money"):0,
                    "discount" => $discount,
                    "dateStart" => $dateStart,
                    "dateEnd" => $dateEnd." 23:59:59",
                    "type" => $this->getSafeParam("type"),
                    "scope" => $this->getSafeParam("scope"),
                    "maxCount" => $this->getSafeParam("maxCount"),
                    "validNum" => $this->getSafeParam("validNum"),
                    "dateCreated" => date("Y-m-d H:i:s")
                );
                $result = Application_Model_CouponBatch::addcouponbatch($no, $arrup);
                $this->view->arrup = $arrup;
                $this->view->ma = $result;
            } else {
                $this->view->ma = 3;
            }
        }
        $this->view->couponType = Application_Model_Coupon::getTypeAll();
        $this->view->arrscope = Application_Model_Coupon::getbyscope();
        $this->view->stype = 6;
    }

    public function endcouponAction() {
        $userid = $this->_getParam("userid");
        $id = $this->_getParam("id");
        $result = Application_Model_Coupon::load($id);
        if ($result) {
            $mailObj = new Application_Model_Mail();
            $mailObj->title = "您有优惠券快过期了";
            $mailObj->userId = $userid;
            $mailObj->message = "尊敬的会员，您有张价值" . $result->money . "元的优惠券(" . $result->no . ")将于" . $result->dateExpired . "过期，请您尽快使用。";
            $mailObj->dateCreated = date("Y-m-d H:i:s");
            $mailObj->type = 1;
            $mailObj->status = 0;
            $mailObj->save();
            echo 1;
        } else {
            echo 2;
        }
        exit;
    }

    public function paybillAction() {
        $this->setTitle("用户支付记录");
        $pagesize = 10;
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->_getParam("quick");
        $dateinbegin = $this->_getParam("dateinbegin");
        $dateinend = $this->_getParam("dateinend");
        $paybill = new Application_Model_Filter_PayBillSearch;
        $paybill->quick = $quick;
        $paybill->dateinbegin = $dateinbegin;
        $paybill->dateinend = $dateinend;
        $paybillItem = Application_Model_PayBill::getFilter($paybill, $pagesize, $pageindex);
        $this->view->paybillall = $paybillItem;
        $this->view->paybilllist = $paybillItem["list"];
        $this->view->quick = $quick;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->stype = 7;
    }
    
    public function paylogAction()
    {
        $this->setTitle("Paypal交易日志记录");
        $pagesize = 10;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $dateinbegin = $this->getSafeParam("dateinbegin");
        $dateinend = $this->getSafeParam("dateinend");
        $paypalStatus = $this->getSafeParam("paypalStatus");
        $status = $this->getSafeParam("status");
        $paylogItem = Application_Model_PaypalLog::getFilter($quick, $dateinbegin,$paypalStatus, $status,$dateinend, $pagesize, $pageindex);
        $this->view->paylogAll = $paylogItem;
        $this->view->paylogList = $paylogItem["list"];
        $this->view->quick = $quick;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->paypalStatus = $paypalStatus;
        $this->view->status = $status;
        $this->view->allPayStatus = Application_Model_PaypalLog::getAllPaypalStatus();
        $this->view->allStatus = Application_Model_PaypalLog::getAllStatus();
        $this->view->stype = 15;        
    }
    
    public function setstatusAction()
    {
        $id = $this->getSafeParam("id");
        $status = $this->getSafeParam("status");
        $payLogItem = Application_Model_PaypalLog::load($id);
        if($payLogItem->Status == Application_Model_PaypalLog::Status_Default){
            $payLogItem->Status = $status;
            $payLogItem->Operator = Uuch_Util::getUserNikeName();
            $payLogItem->save();
            Application_Model_Operate::addOperate(0, null, $payLogItem->id, "修改Paypal日志记录状态为：".$payLogItem->StatusName, $payLogItem->txnId);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }

    public function editaddressAction() {
        $id = $this->_getParam("id");
        $address = Application_Model_address::load($id);
        if ($address) {
            if ($this->_getParam("operate") == "editaddress") {
                $state = (int) $this->_getParam("state");
                $address->linkMan       = $this->_getParam("linkMan");
                $address->state         = $state;
                $address->province      = $this->_getParam("province");
                $address->city          = $this->_getParam("city");
                $address->street        = $this->_getParam("street");
                $address->mobile        = $this->_getParam("mobile");
                $address->phone         = $this->_getParam("phone");
                $address->city_name     = $this->_getParam("city_name");
                $address->province_name = $this->_getParam("province_name");
                $address->postcode      = $this->_getParam("postcode");
                
                if($state == 231){
                    //中国
                    $address->type      = 1;
                }else{
                    if($state == 7){
                        //德国需要此信息
                        $address->eori  = $this->_getParam("eori");
                    }else{
                        $address->eori  = '';
                    }
                    $address->type      = 0;
                }

                $address->save();
                $this->view->ma = 1;
                Application_Model_Operate::addOperate(0, null, $address->id, "修改用户收获地址", $address->userId);
            }
            $this->view->address = $address;
        } else {
            $this->view->ma = 2;
        }
        $this->view->state = Application_Model_city::byAllcity(array('parentId' => 0));
    }

    public function deladdressAction() {
        $idlist = $this->_getParam("idlist");
        $arrid = explode(",", $idlist);
        for ($i = 0; $i < count($arrid); $i++) {
            $addressItem = Application_Model_address::load($arrid[$i]);
            if ($addressItem) {
                Application_Model_Operate::addOperate(0, null, $addressItem->id, "删除用户收获地址", $addressItem->userId);
                $addressItem->delete();
            }
        }
        echo 1;
        exit;
    }

    //删除优惠券
    public function delcouponAction() {
            $idlist = $this->_getParam("idlist");
            $arrid = explode(",", $idlist);
            $a = '';
            for ($i = 0; $i < count($arrid); $i++) {
                $couponItem = Application_Model_Coupon::load($arrid[$i]);
                if ($couponItem) {
                    Application_Model_Operate::addOperate(0, null, $couponItem->id, "删除优惠券", $couponItem->userId);
                    $couponItem->delete();
                    $a = 1;
                }
            }
            //$this->json(1);
            header("Content-type: text/html; charset=utf-8"); 
            if($a = 1){
                echo "<script> alert('删除成功');location.href='http://admin1.uuch.com/admini/manageuser/coupon/';</script>";
            }else{
                echo "<script> alert('删除失败');location.href='http://admin1.uuch.com/admini/manageuser/coupon/';</script>";
            }
            //echo json_encode($a);
            //Header("Location: http://admin1.uuch.com/admini/manageuser/coupon/");
            exit; 
        }

    public function customerlistAction() {
        $this->setTitle("客户排名");
        $users = new Application_Model_DbTable_User();
        $customers = $users->customerList();
        $this->view->customers = $customers;
        $this->view->stype = 8;
    }

    /**
     * 会员投诉列表
     */
    public function complaintsAction() {
        $stype = 9;
        $this->setTitle("会员投诉");
        $pagesize = $this->getSafeParam('pagesize');
        $start = $this->getSafeParam('start');
        $timescope = $this->getSafeParam("timescope");
        $strart = date("m") % 3 == 0 ? date("m") / 3 : ((int) (date("m") / 3) + 1);
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        $isDelete = $this->getSafeParam("isDelete");
        $status = $this->getSafeParam("status");
        if ($status == 1) {
            $stype = 101;
        }
        if ($isDelete == 1) {
            $stype = 10;
        }
        if (!$pagesize) {
            $pagesize = 10;
        }
        if (!$start) {
            $start = 0;
        }
        $roleid = Uuch_Util::getRoleId();
        if($roleid>1&&$status=='0'){
            $this->message("没有权限访问", "", "/admini/manageuser/complaints/status/1");
        }
        if ($timescope != null) {
            switch ($timescope) {
                case "yesterday":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
                    break;
                case "day3":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 3, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case 'lastweek':
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
                    break;
                case "week":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 7, date("Y")));
                    break;
                case "month":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("t"), date("Y")));
                    break;
                case "lastmonth":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, cal_days_in_month(CAL_GREGORIAN, date("m") - 1, date("Y")), date("Y")));
                    break;
                case "quarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 + 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, $strart * 3, cal_days_in_month(CAL_GREGORIAN, $strart * 3, date("Y")), date("Y")));
                    break;
                case "lastquarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 - 2, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3, cal_days_in_month(CAL_GREGORIAN, ($strart - 1) * 3, date("Y")), date("Y")));
                    break;
                case "year":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, 12, cal_days_in_month(CAL_GREGORIAN, 12, date("Y")), date("Y")));
                    break;
                case "lastyear":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y") - 1));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, 12, cal_days_in_month(CAL_GREGORIAN, 12, date("Y") - 1), date("Y") - 1));
                    break;
                case "day7":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 6, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case "day30":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 30, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                default :
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 90, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
            }
        } else {
            if ($starttime == null && $endtime == "") {
                $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y") - 1));
                $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
            }
        }
        $complainsSearch = new Application_Model_Filter_ComplaintsSearch();
        $complainsSearch->quick = $this->getSafeParam("quick");
        $complainsSearch->starttime = $starttime;
        $complainsSearch->endtime = $endtime;
        $complainsSearch->status = $status;
        $complainsSearch->isDelete = $isDelete;
        $this->view->res = Application_Model_Complaints::getMyByFilter($complainsSearch, $pagesize, $start);
        $this->view->list = $this->view->res['list'];
        $this->view->quick = $complainsSearch->quick;
        $this->view->starttime = $complainsSearch->starttime;
        $this->view->endtime = $complainsSearch->endtime;
        $this->view->isDelete = $complainsSearch->isDelete;
        $this->view->status = $complainsSearch->status;
        $this->view->pagesize = $pagesize;
        $this->view->stype = $stype;
    }

    /**
     * 删除投诉
     */
    public function delcomplaintsAction() {
        $id = $this->getSafeParam("id");
        $opt = Application_Model_Complaints::load($id);
        $opt->IsDelete = 1;
        Application_Model_Operate::addOperate(0, null, $id, "删除用户投诉", $opt->UserId);
        $opt->save();
        $this->json(1);
    }

    /**
     * 回复投诉
     */
    public function replyAction() {
        $id = $this->getSafeParam("id");
        $content = $this->getSafeParam("content");
        $opt = Application_Model_Complaints::load($id);
        $title = iconv_strlen($opt->Title) >= 32 ? mb_substr($opt->Title, 0, 32, 'UTF-8') . '......' : $opt->Title;
        $opt->Reply = $content;
        $opt->Status = 1;
        $opt->Operator = Uuch_Util::getUserNikeName();
        $opt->save();
        //发站内信回复客户        
        $opt->sendMessage($opt->Title, $opt->UserId, $content);
        //记录操作日志
        Application_Model_Operate::addOperate(0, null, $id, "回复用户投诉", $opt->UserId);
        $this->json(1);
    }

    /**
     * 获取投诉详情
     */
    public function complaintdetailAction() {
        $id = $this->getSafeParam("id");
        $opt = Application_Model_Complaints::load($id);
        $this->json(array('content' => $opt->Content, 'reply' => $opt->Reply, 'id' => $opt->Id, 'operator' => $opt->Operator));
    }

    /**
     * 线下汇款列表
     */
    public function transferAction() {
        $stype = 11;
        $this->setTitle("会员线下汇款");
        $pagesize = $this->getSafeParam('pagesize');
        $start = $this->getSafeParam('start');
        $timescope = $this->getSafeParam("timescope");
        $strart = date("m") % 3 == 0 ? date("m") / 3 : ((int) (date("m") / 3) + 1);
        $starttime = $this->getSafeParam("starttime");
        $endtime = $this->getSafeParam("endtime");
        $isDelete = $this->getSafeParam("isDelete");
        $status = $this->getSafeParam("status");
        if ($isDelete == 1) {
            $stype = 13;
        } else if ($status == 1) {
            $stype = 12;
        }
        if (!$pagesize) {
            $pagesize = 10;
        }
        if (!$start) {
            $start = 0;
        }
        if ($timescope != null) {
            switch ($timescope) {
                case "yesterday":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
                    break;
                case "day3":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 3, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case 'lastweek':
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
                    break;
                case "week":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 7, date("Y")));
                    break;
                case "month":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("t"), date("Y")));
                    break;
                case "lastmonth":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, cal_days_in_month(CAL_GREGORIAN, date("m") - 1, date("Y")), date("Y")));
                    break;
                case "quarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 + 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, $strart * 3, cal_days_in_month(CAL_GREGORIAN, $strart * 3, date("Y")), date("Y")));
                    break;
                case "lastquarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 - 2, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3, cal_days_in_month(CAL_GREGORIAN, ($strart - 1) * 3, date("Y")), date("Y")));
                    break;
                case "year":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, 12, cal_days_in_month(CAL_GREGORIAN, 12, date("Y")), date("Y")));
                    break;
                case "lastyear":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y") - 1));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, 12, cal_days_in_month(CAL_GREGORIAN, 12, date("Y") - 1), date("Y") - 1));
                    break;
                case "day7":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 6, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case "day30":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 30, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                default :
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 90, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
            }
        } else {
            if ($starttime == null && $endtime == "") {
                $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y") - 1));
                $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
            }
        }
        $remittanceSearch = new Application_Model_Filter_RemittanceSearch();
        $remittanceSearch->quick = $this->getSafeParam("quick");
        $remittanceSearch->starttime = $starttime;
        $remittanceSearch->endtime = $endtime;
        $remittanceSearch->isDelete = $isDelete;
        $remittanceSearch->status = $status;
        $this->view->res = Application_Model_Remittance::getMyByFilter($remittanceSearch, $pagesize, $start);
        $this->view->list = $this->view->res['list'];
        $this->view->quick = $remittanceSearch->quick;
        $this->view->starttime = $remittanceSearch->starttime;
        $this->view->endtime = $remittanceSearch->endtime;
        $this->view->isDelete = $remittanceSearch->isDelete;
        $this->view->status = $remittanceSearch->status;
        $this->view->pagesize = $pagesize;
        $this->view->stype = $stype;
    }

    /**
     * 获取汇款凭证（图片）
     */
    public function transferdetailAction() {
        $id = $this->getSafeParam("id");
        $opt = Application_Model_Remittance::load($id);
        $this->json(array('picture' => $opt->Picture));
    }

    /**
     * 删除汇款记录
     */
    public function deltransferAction() {
        $id = $this->getSafeParam("id");
        $opt = Application_Model_Remittance::load($id);
        $opt->IsDelete = 1;
        Application_Model_Operate::addOperate(0, null, $id, "删除汇款记录", $opt->userId);
        $opt->save();
        $this->json(1);
    }

    /**
     * 处理线下汇款
     */
    public function processedtransferAction() {
        $id = $this->getSafeParam("id");
        $opt = Application_Model_Remittance::load($id);
        $opt->Status = Application_Model_Remittance::STATUS_PROCESSED;
        Application_Model_Operate::addOperate($opt->Money, null, $id, "处理线下汇款记录", $opt->userId);
        $opt->save();
        $this->json(1);
    }

}

